import 'dart:developer';
import 'dart:io';

import 'package:advertising_id/advertising_id.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:device_identity/device_identity.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter_udid/flutter_udid.dart';
import 'package:uuid/uuid.dart';
import 'package:logger/logger.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:ball_lion/utils/app_config.dart';
import 'package:ball_lion/utils/sp_utils.dart';
import 'package:ball_lion/server/http/rsa_util.dart';

class RequestHeader {
  static var appid = (Platform.isIOS ? "qs-ios" : "qs-an");
  static String channel = "";
  //App版本号
  static String version = "";
  static String os = "";
  static String osVersion = "";
  //经纬度 未用到
  static String pnt = "";
  static String network = "";
  //手机型号
  static String model = "";
  static String uuid = "";
  static String udid = "";
  static String idfa = "";
  static String imei = "";
  static String oaid = "";
  static String pushId = "";
  static String anid = "";
  static String brand = "";
  //后端返回服务器环境
  static String env = "";

  static Map<String, String> toJson() {
    return {
      "appid": RequestHeader.appid,
      "channel": RequestHeader.channel,
      "version": RequestHeader.version,
      "os": RequestHeader.os,
      "osVersion": RequestHeader.osVersion,
      "pnt": RequestHeader.pnt,
      "network": RequestHeader.network,
      "model": RequestHeader.model,
      "uuid": RequestHeader.uuid,
      "udid": RequestHeader.udid,
      "idfa": RequestHeader.idfa,
      "imei": RequestHeader.imei,
      "oaid": RequestHeader.oaid,
      "pushId": RequestHeader.pushId,
      "anid": RequestHeader.anid,
      "brand": RequestHeader.brand
    };
  }

  static print() {
    Logger().d("header ext info = ${toJson()}");
    log("header ext info = ${toJson()}");
  }

  static preInit() async {
    await RsaUtil.init();
    await getPlatformInfo();
  }

  static init() async {
    if (SpUtils.appUdid != null) {
      RequestHeader.udid = SpUtils.appUdid!;
    } else {
      SpUtils.appUdid = await FlutterUdid.udid;
      RequestHeader.udid = SpUtils.appUdid!;
    }
    if (SpUtils.appUuid != null) {
      RequestHeader.uuid = SpUtils.appUuid!;
    } else {
      SpUtils.appUuid = const Uuid().v1();
      RequestHeader.uuid = SpUtils.appUuid!;
    }
    await packageInfo();
    await getDevideInfo();
    await getIDFA();
    await getAndroidId();
    RequestHeader.print();
  }

  static DateTime getIdfaTime = DateTime.fromMillisecondsSinceEpoch(0);

  static getPlatformInfo() async {
    if (Platform.isAndroid) {
      RequestHeader.os = "android";
      RequestHeader.channel = AppConfig.config.channel ?? '';
    } else if (Platform.isIOS) {
      RequestHeader.os = "ios";
      RequestHeader.channel = "sn10000";
    } else {
      RequestHeader.os = "other";
    }
  }

  static packageInfo() async {
    final package = await PackageInfo.fromPlatform();
    RequestHeader.version = package.version;
  }

  static getDevideInfo() async {
    if (Platform.isAndroid) {
      final deviceInfo = await DeviceInfoPlugin().androidInfo;
      RequestHeader.model = deviceInfo.model;
      RequestHeader.osVersion = deviceInfo.version.release;
      RequestHeader.brand = deviceInfo.brand;
    }
    if (Platform.isIOS) {
      final deviceInfo = await DeviceInfoPlugin().iosInfo;
      RequestHeader.model = deviceInfo.utsname.nodename ?? "";
      RequestHeader.osVersion = deviceInfo.systemVersion ?? "";
    }
  }

  static getIDFA() async {
    if (Platform.isAndroid) {
      return;
    }
    if (RequestHeader.idfa.isNotEmpty) {
      return;
    }
    if (DateTime.now().difference(getIdfaTime).inSeconds < 60) {
      return;
    }
    getIdfaTime = DateTime.now();
    try {
      final auth = await AdvertisingId.isLimitAdTrackingEnabled;
      final id = await AdvertisingId.id(true);
      log("idfa = $id auth = $auth");
      RequestHeader.idfa = id ?? "";
    } catch (err) {
      log("get idfa error $err");
    }
  }

  static getAndroidId() async {
    if (Platform.isIOS) {
      return;
    }
    await DeviceIdentity.register();
    String imei = await DeviceIdentity.imei;
    String oaid = await DeviceIdentity.oaid;
    String anid = await DeviceIdentity.androidId;
    RequestHeader.imei = imei;
    RequestHeader.oaid = oaid;
    RequestHeader.anid = anid;
  }

  static setNetworkInfo(List<ConnectivityResult> result) {
    if (result.contains(ConnectivityResult.wifi)) {
      RequestHeader.network = "wifi";
    } else if (result.contains(ConnectivityResult.mobile)) {
      RequestHeader.network = "mobile";
    } else if (result.contains(ConnectivityResult.none)) {
      RequestHeader.network = "none";
    } else {
      RequestHeader.network = "other";
    }
  }
}
